import { EcsSystem, filter } from 'db://pkg/@gamex/cc-ecs';
import { TimerComponent } from '../component/TimerComponent';
import { TweenComponent } from '../component/TweenComponent';

export class TimerSystem extends EcsSystem {
    private tweenFilter = filter.all(TweenComponent);
    private timerFilter = filter.all(TimerComponent);

    protected beforeExecute(dt: number) {
        this.query(this.timerFilter).forEach((entity) => {
            entity.getComponents(TimerComponent).forEach((timer) => {
                if (TimerComponent.systemExecute(timer, dt) === 0) {
                    timer.destroy();
                }
            });
        });

        this.query(this.tweenFilter).forEach(entity => {
            entity.getComponents(TweenComponent).forEach(tween => {
                if (TweenComponent.systemExecute(tween, dt) === 0) {
                    tween.destroy();
                }
            });
        });
    }
}